//
//  ImageTool.h
//  E-Publishing
//
//  Created by 李 雷川 on 13-4-15.
//
//

#import "ImageExt.h"

@implementation UIImage (UIImageExt)


+ (UIImage *)screenshotForScreenImage
{
    UIView *view = [UIApplication sharedApplication].keyWindow;
    
	UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [[UIScreen mainScreen] scale]);
    
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CGFloat barHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
    
    if ([[UIApplication sharedApplication] isStatusBarHidden] == NO)
    {
        CGFloat scale = [[UIScreen mainScreen] scale];
        CGFloat scaledY = barHeight * scale;
        CGFloat scaledWidth = view.bounds.size.width * scale;
        CGFloat scaledHeight = ((view.bounds.size.height - barHeight) * scale);
        
        CGRect rect = (CGRect) { {0, scaledY }, { scaledWidth, scaledHeight } };
        
        CGImageRef imageRef = CGImageCreateWithImageInRect(screenshotImage.CGImage, rect);
        screenshotImage = [UIImage imageWithCGImage:imageRef scale:screenshotImage.scale orientation:screenshotImage.imageOrientation];
    }
    
    
    return screenshotImage;
}


+ (UIImage *)screenshotImageForView:(UIView *)view{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [[UIScreen mainScreen] scale]);
    CALayer *layer = view.layer;
    if (layer) {
        [layer renderInContext:UIGraphicsGetCurrentContext()];
    }
    UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return screenshotImage;
}

- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
    // 绘制改变大小的图片
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    // 返回新的改变大小后的图片
    return scaledImage;
}



- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
    UIImage *sourceImage = self;
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    
    if (CGSizeEqualToSize(imageSize, targetSize) == NO)
    {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        
        if (widthFactor > heightFactor)
            scaleFactor = widthFactor; // scale to fit height
        else
            scaleFactor = heightFactor; // scale to fit width
        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;
        
        // center the image
        if (widthFactor > heightFactor)
        {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }
        else
            if (widthFactor < heightFactor)
            {
                thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
            }
    }
    
    UIGraphicsBeginImageContext(targetSize); // this will crop
    
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    if(newImage == nil)
        NSLog(@"could not scale image");
    
    //pop the context to get back to the default
    UIGraphicsEndImageContext();
    return newImage;
}

- (UIImage*)getGrayImage:(UIImage*)sourceImage
{
    

    
    int width = sourceImage.size.width;
    int height = sourceImage.size.height;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate (nil,width,height,8,0,colorSpace,kCGImageAlphaNone);
    CGColorSpaceRelease(colorSpace);
    if (context == NULL) {
        return nil;
    }
    CGContextDrawImage(context,CGRectMake(0, 0, width, height), sourceImage.CGImage);
    CGImageRef grayImageRef = CGBitmapContextCreateImage(context);
    UIImage *grayImage = [UIImage imageWithCGImage:grayImageRef];
    CGContextRelease(context);
    CGImageRelease(grayImageRef);
    return grayImage;
}

- (UIImage *)scaleAspectFitSize:(UIImage *)image size:(CGSize)size{
    float h;
    float w;
    float scale=image.size.width/image.size.height;
    if (image.size.width<=size.width &&image.size.height<=size.height )
    {//小图
        w = image.size.width;
        h = image.size.height;
    }
    else if (image.size.width<=size.width &&image.size.height>size.height )
    {//高图
        h = size.height;
        w = h*scale;
    }
    else if (image.size.width>size.width &&image.size.height<=size.height )
    {//宽图
        w = size.width;
        h = w/scale;
    }
    else if (image.size.width>=size.width &&image.size.height>size.height )
    {//大图
        if (scale>1) {//宽图片
            w = size.width;
            h = w/scale;
        }else{
            h = size.height;
            w = h*scale;
        }
    }
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(CGSizeMake(w,h));
    // 绘制改变大小的图片
    [image drawInRect:CGRectMake(0, 0, w,h)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    // 返回新的改变大小后的图片
    return scaledImage;
}

@end
